package service;

import beans.Drivers;
import beans.Vehicle;
import db.DriversDao;
import jakarta.servlet.http.Part;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

public class DriversService {
    private DriversDao driversDao;

    public DriversService() {
        this.driversDao = new DriversDao();
    }

    public boolean addDriver(Drivers driver, Part imagePart) {

        try {
            if (driversDao.getDriverById(driver.getDriverId()) != null) {
                return false; // ID已存在
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        System.out.println("adddriver_service");
        // 处理文件上传
        if (imagePart != null && imagePart.getSize() > 0) {
            try {
                String uploadPath = "C:/uploads"; // 修改为实际存储路径
                Files.createDirectories(Paths.get(uploadPath));
                String fileName = UUID.randomUUID().toString() + "_" + imagePart.getSubmittedFileName();
                Path filePath = Paths.get(uploadPath, fileName);
                try (InputStream input = imagePart.getInputStream()) {
                    Files.copy(input, filePath, StandardCopyOption.REPLACE_EXISTING);
                }
                driver.setDriverLicenseImage(filePath.toString());
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        // 调用DAO保存数据
        return driversDao.addDriver(driver);
    }

    // 获取所有司机
    public List<Drivers> getAllDrivers() {
        return driversDao.getAllDrivers();
    }

    // 获取所有可用司机
    public List<Drivers> getAllAvailableDrivers() {
        try {
            return driversDao.getAvailableDrivers();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    //搜搜司机
    public List<Drivers> searchDrivers(
            String driverId,
            String driverName,
            String phoneNumber,
            String gender,
            String status
    ) {
        try {
            // 校验DriverID是否为数字
            if (driverId != null && !driverId.isEmpty()) {
                Integer.parseInt(driverId);
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("司机ID必须为数字");
        }
        return driversDao.searchDrivers(driverId, driverName, phoneNumber, gender,status);
    }

    // 更新司机信息
    public boolean updateDriver(Drivers driver) {
        try {
            return driversDao.updateDriver(driver);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public Drivers getDriverById(int driver) {
        try {
            return driversDao.getDriverById(driver);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 获取司机姓名
    public String getDriverNameById(int driverId) {
        try {
            Drivers driver = driversDao.getDriverById(driverId);
            return driver != null ? driver.getDriverName() : "未知司机";
        } catch (SQLException e) {
            e.printStackTrace();
            return "未知司机";
        }
    }

    // 删除司机
    public boolean deleteDriver(int driverId) {
        try {
            return driversDao.deleteDriver(driverId);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 获取所有司机ID
    public List<Integer> getAllDriverIds() {
        try {
            return driversDao.getAllDriverIds();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 新增方法：修改密码
    public boolean changePassword(int driverId, String oldPassword, String newPassword) throws SQLException {
        // 1. 验证原密码
        Drivers driver = driversDao.getDriverById(driverId);
        if (driver == null || !driver.getPassword().equals(oldPassword)) {
            return false; // 原密码不正确或司机不存在
        }

        // 2. 更新密码
        // 假设 DriversDao 中有一个 updatePassword(int driverId, String newPassword) 方法
        return driversDao.updatePassword(driverId, newPassword);
    }
}